import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store';
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  //登陆
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  //404
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  //工作台
  {
    path: '/',
    redirect: '/datav',
    hidden: true
  },
  //首页
  {
    path:'/datav',
    name:'datav',
    component: () => import('@/views/datav/index'),
    meta: { title: '首页大屏', icon: 'el-icon-s-platform'},
  },
  //首页大屏
  {
    path:'/datav2',
    name:'datav2',
    hidden: true,
    component: () => import('@/views/datav/index2'),
    meta: { title: '首页大屏', icon: 'el-icon-s-platform'},
  },
  //项目大屏
  {
    path:'/datap',
    name:'datap',
    hidden: true,
    component: () => import('@/views/datav/project'),
    meta: { title: '项目大屏', icon: 'el-icon-s-platform'},
  },
  {
    path:'/project',
    component: Layout,
    redirect: '/project/index',
    children:[
      {
        path: 'index',
        name: 'project',
        component: () => import('@/views/project/index'),
        meta: { title: '项目中心', icon: 'el-icon-office-building'},
      }
    ]
  },
  //组织架构
  {
    path:'/org',
    component: Layout,
    redirect: '/org/index',
    meta: { title: '组织架构', icon: 'el-icon-coin'},
    children:[
      {
        path: 'index',
        name: 'org',
        component: () => import('@/views/org/index'),
        meta: { title: '组织及部门'},
      },
      {
        path: 'user',
        name: 'user',
        component: () => import('@/views/user/index'),
        meta: { title: '用户管理'},
      },
      {
        path: 'post',
        name: 'post',
        component: () => import('@/views/post/index'),
        meta: { title: '岗位管理'},
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  //mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

router.beforeEach((to, from, next) => {
  let token = ''
  try{
    token = store.getters.userinfo.access_token
    store.commit('user/SET_Token', token)
  }catch{}
  if(token){
    next()
  }else{
    if (to.path == '/datav' && !token) {
        next({path:'/login'})
    }else{
        next()
    }
    // window.location.href = `${process.env.VUE_APP_ZDB_URL}/user/login?redirect=${window.location.href}`
  }
})

function getUrlSearch(name) {
  // 未传参，返回空
  if (!name) return null;
  // 查询参数：先通过search取值，如果取不到就通过hash来取
  var after = window.location.search;
  after = after.substr(1) || window.location.hash.split('?')[1];
  // 地址栏URL没有查询参数，返回空
  if (!after) return null;
  // 如果查询参数中没有"name"，返回空
  if (after.indexOf(name) === -1) return null;
 
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
  // 当地址栏参数存在中文时，需要解码，不然会乱码
  var r = decodeURI(after).match(reg);
  // 如果url中"name"没有值，返回空
  if (!r) return null;
 
  return r[2];
}

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
